﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Rendering;

namespace GISSystem.Class
{
    /// <summary>
    /// 这个类主要用于实现某些整体的功能函数，非基于对象的
    /// </summary>
    public class Function
    {
        #region 返回一个图层的符号图像
        /// <summary>
        /// 返回一个图层的符号图像
        /// </summary>
        /// <param name="pLayer">要素图层</param>
        /// <param name="bmp">输出的图像</param>
        /// <returns></returns>
        static public void GetLayerImage(IFeatureLayer pLayer, ref Bitmap bmp)
        {
            if (pLayer == null)
            {
                throw new Exception("没有得到图层信息。");
            }

            //获得bmp的句柄
            Graphics g = Graphics.FromImage(bmp);
            IntPtr hdc = new IntPtr();
            hdc = g.GetHdc();

            //设置一个Envelope对象，在这个区域内绘图
            IEnvelope pEnv = new EnvelopeClass();
            pEnv.PutCoords(0, 0, bmp.Width, bmp.Height);

            //进行设备转换
            IDisplayTransformation pDisTrans = new DisplayTransformationClass();
            tagRECT deviceRect = new tagRECT();
            deviceRect.left = 0;
            deviceRect.right = 0;
            deviceRect.right = bmp.Width;
            deviceRect.bottom = bmp.Height;

            pDisTrans.VisibleBounds = pEnv;
            pDisTrans.Bounds = pEnv;
            pDisTrans.set_DeviceFrame(ref deviceRect);
            pDisTrans.Resolution = 96;

            ITransformation pTrans = pDisTrans as ITransformation;

            ISymbol pSymbol = GetLayerSymbol(pLayer);  //获得图层的符号

            IGeometry pGeometry = CreateSymbolShape(pSymbol, pEnv);  //设置符号图像的形状

            //绘图
            pSymbol.SetupDC(hdc.ToInt32(), pTrans);
            pSymbol.Draw(pGeometry);
            pSymbol.ResetDC();
            g.ReleaseHdc(hdc);
            g.Dispose();
        }

        /// <summary>
        /// 获得某一图层的符号
        /// </summary>
        /// <param name="pLayer">要素图层</param>
        /// <returns>图层符号</returns>
        public static ISymbol GetLayerSymbol(IFeatureLayer pLayer)
        {
            if (pLayer == null)
            {
                throw new Exception("没有得到图层信息。");
            }

            ISymbol pSymbol; //返回的符号对象

            IGeoFeatureLayer pGeoFL = pLayer as IGeoFeatureLayer;
            ISimpleRenderer pSimpleRenderer = pGeoFL.Renderer as ISimpleRenderer;

            if (pSimpleRenderer == null)
            {
                throw new Exception("没有获得图层的符号信息。");
            }

            pSymbol = pSimpleRenderer.Symbol;

            return pSymbol;
        }

        /// <summary>
        /// 设置图层的预览符号形状
        /// </summary>
        /// <param name="pSymbol">符号</param>
        /// <param name="pEnv">Envelop区域</param>
        /// <returns></returns>
        static private IGeometry CreateSymbolShape(ISymbol pSymbol, IEnvelope pEnv)
        {
            if (pSymbol is IMarkerSymbol)  //pSymbol是一个点类的符号
            {
                IArea pArea = pEnv as IArea;
                return pArea.Centroid; //返回一个区域的中心点
            }
            else if (pSymbol is ILineSymbol || pSymbol is ITextSymbol)  //pSymbol是一个线类的符号或者字符符号
            {
                //绘制一条直线，贯穿整个Envelope区域
                IPolyline pPolyline = new PolylineClass();
                IPoint pFromPt = new PointClass();
                pFromPt.PutCoords(pEnv.XMin, pEnv.YMin + pEnv.Height / 2);

                IPoint pToPt = new PointClass();
                pToPt.PutCoords(pEnv.XMax, pEnv.YMin + pEnv.Height / 2);

                pPolyline.FromPoint = pFromPt;
                pPolyline.ToPoint = pToPt;

                return pPolyline;
            }
            else //其他情况返回整个Envelope
            {
                return pEnv;
            }
        }
        #endregion

        #region 设置界面的颜色
        /// <summary>
        /// 设置界面的颜色风格
        /// </summary>
        /// <param name="frm">窗口名称</param>
        /// <param name="color">颜色</param>
        public static void SetFormStyle(Form frm, Color color)
        {
            if (color == Color.Blue)
            {
                RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(frm, eOffice2007ColorScheme.Blue);
            }
            else if (color == Color.Black)
            {
                RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(frm, eOffice2007ColorScheme.Black);
            }
            else if (color == Color.Silver)
            {
                RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(frm, eOffice2007ColorScheme.Silver);
            }
            else
            {
                RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(frm, eOffice2007ColorScheme.Silver, color);
            }
        }
        #endregion

        /// <summary>
        /// 获取一些配置信息
        /// </summary>
        public static void GetConfiguration()
        {
            //获取全局地图窗口的一些信息
            Configuration.ShowOverviewForm = Ini.GetOverviewFormShow();
            Configuration.OverviewFormLocation = Ini.GetOverviewFormLocation();
            Configuration.OverviewFormHeight = Ini.GetOverviewFormHeight();
            Configuration.OverviewFormWidth = Ini.GetOverviewFormWidth();
        }
    }
}
